package com.nuoniu.sibanyun.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.project.SysRdtField;
import com.nuoniu.sibanyun.entity.project.SysRdtForm;
import com.nuoniu.sibanyun.service.project.ISysRdtFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目表单管理
 * @auther 小啊刚
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/sdt/form")
@Api(tags = "项目表单管理")
public class SysRdtFormController {

    @Autowired
    private ISysRdtFormService formService;

    @ApiOperation("一键导入数据")
    @PostMapping("importData")
    public HttpResult importData(Integer projectId) {
        formService.importData(projectId);
        return HttpResult.ok();
    }

    @ApiOperation("通过项目获取表列表")
    @GetMapping("getListByProjectId")
    public HttpResult getListByProjectId(Integer projectId,String keyWord) {
        return HttpResult.ok(formService.getListByProjectId(projectId,keyWord));
    }

    @ApiOperation("通过菜单和项目 获取表列表")
    @GetMapping("getFormList")
    public HttpResult getList(Integer menuId,Integer projectId) {
        return HttpResult.ok(formService.getList(menuId,projectId));
    }

    @ApiOperation("获取字段列表")
    @GetMapping("getFieldList")
    public HttpResult<SysRdtField> getFieldList(Integer formId) {
        return HttpResult.ok(formService.getFieldList(formId));
    }

    @ApiOperation("保存表")
    @PostMapping("saveForm")
    public HttpResult saveForm(@RequestBody SysRdtForm form) {
        if(null == form.getProjectId())throw new NuoNiuException("请选择项目");
        formService.saveOrUpdate(form);
        return HttpResult.ok();
    }

    @ApiOperation("删除表")
    @DeleteMapping("deleteForm")
    public HttpResult deleteForm(Long id, String menuId) {
        formService.deleteById(id,menuId);
        return HttpResult.ok();
    }

    @ApiOperation("保存字段")
    @PostMapping("saveField")
    public HttpResult<SysRdtField> saveField(@RequestBody List<SysRdtField> fields) {
        formService.saveField(fields);
        return HttpResult.ok();
    }

    @ApiOperation("删除字段")
    @DeleteMapping("deleteField")
    public HttpResult deleteField(Long id) {
        formService.deleteField(id);
        return HttpResult.ok();
    }
}
